import {createApp} from 'vue'
import App from './App.vue'
import router from './router'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

require('./mock/index')
// 引入仓库
import store from "@/store";
import HttpClient from "@/utils/HttpClient";


const app = createApp(App)

for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}
app.config.globalProperties.$HttpClient = HttpClient

app.use(store)
app.use(ElementPlus)
app.use(router)
app.mount('#app')

import userStore from "@/store/modules/user";

interface userInfoType {
    userName: string
}

router.beforeEach(async (to, from, next) => {
    if (from.path === '/' || from.path === '/login') {
        const token = sessionStorage.getItem('token')
        const userInfo = userStore().getUserInfo as any
        console.log(111333)
        if (token) { // 判断是否登录 当token存在时说明已经登陆过 当userName不存在时说明页面刷新了需要重新加载路由
            if (!userInfo['userName']) {
                await userStore().saveMenuList()
            }
        }
        console.log(router.getRoutes())
        if (to.matched.length === 0) {
            router.push(to.path)
        }
        next()
    } else {
        next()
    }
})
